package com.tencent.iov.cowin.befsale.clue.component;

import com.tencent.cloud.iov.common.api.DataMaskingService;
import com.tencent.cloud.iov.common.crypt.impl.IdCardCryptImpl;
import com.tencent.cloud.iov.common.crypt.impl.MobileCryptImpl;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: Caychen
 * @Date: 2022/2/14 10:12
 * @Description:    解密脱敏工具类
 */
//@Component
public class MaskComponent {

//    @Autowired
    private DataMaskingService dataMaskingService;

    public MaskComponent(DataMaskingService dataMaskingService) {
        this.dataMaskingService = dataMaskingService;
    }

    /**
     * 加密
     *
     * @param content
     * @return
     */
    public String encryptAES128(String content) {
        String encryptAES128 = dataMaskingService.encryptAES128(content);
        return encryptAES128;
    }

    /**
     * 解密
     *
     * @param content
     * @return
     */
    public String decryptAES128(String content) {
        String decryptAES128 = dataMaskingService.decryptAES128(content);
        return decryptAES128;
    }

    /**
     * 手机号解密且脱敏
     *
     * @param content
     * @return
     */
    public String mobileDeCryptWithDesensitize(String content) {
        if (StringUtils.isNotBlank(content)) {
            String phone = this.decryptAES128(content);
            return new MobileCryptImpl().encrypt(phone);
        } else {
            return content;
        }
    }

    /**
     * 手机号解密不脱敏
     *
     * @param content
     * @return
     */
    public String mobileDeCryptWithoutDesensitize(String content) {
        if (StringUtils.isNotBlank(content)) {
            String phone = this.decryptAES128(content);
            return phone;
        } else {
            return content;
        }
    }

    /**
     * 身份证解密且脱敏
     *
     * @param content
     * @return
     */
    public String idCardDeCryptWithDesensitize(String content) {
        if (StringUtils.isNotBlank(content)) {
            String idCard = this.decryptAES128(content);
            return new IdCardCryptImpl().encrypt(idCard);
        } else {
            return content;
        }
    }

    /**
     * 身份证号解密不脱敏
     *
     * @param content
     * @return
     */
    public String idCardDeCryptWithoutDesensitize(String content) {
        if (StringUtils.isNotBlank(content)) {
            String idCard = this.decryptAES128(content);
            return idCard;
        } else {
            return content;
        }
    }
}
